package com.jeesite.modules.dataScreen.ssm;

import com.alibaba.fastjson2.JSONObject;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

import static com.jeesite.modules.dataScreen.ssm.SmsTmplateRule.build;

@Getter
public enum SmsTmplateEnum {
    //突发事件信息专报,{time,20}，在{addr,5}路段/水域，{county,5}{town,7}，发生一起{eventName,20}。事件详情如下：{eventDetail,30}。
    BREAK_OUT_EVENT("2091012034938", "{time}，在{addr}路段/水域，{county}{town}，发生一起{eventName}。事件详情如下：{eventDetail}。"
    ),

    //预案启动通知，市交通运输委{姓名，4}决定于{时间，20}，启动{预案，10}应急预案，请收到该通知的单位和人员严格按照{预案，10}预案要求，做好相关工作，如有疑问请致电{号码：15}。
    PRE_PLAN_START("2091012034935", "市交通运输委{name}决定于{time}，启动{yuan}应急预案，请收到该通知的单位和人员严格按照{yuan}预案要求，做好相关工作，如有疑问请致电{phone}。"
    );

    private final String id;
    private final String template;

    SmsTmplateEnum(String id, String template) {
        this.id = id;
        this.template = template;


    }

    private static final Map<String, SmsTmplateEnum> templateMap = new HashMap<>();
    private static final Map<String, SmsTmplateRule[]> ruleMap = new HashMap<>();

    static {
        for (SmsTmplateEnum value : SmsTmplateEnum.values()) {
            templateMap.put(value.getId(), value);
        }

        ruleMap.put(BREAK_OUT_EVENT.getId(), new SmsTmplateRule[]{
                build("time", "时间", false, 20),
                build("addr", "地址", true, 5),
                build("county", "区县", true, 5),
                build("town", "乡镇", true, 7),
                build("eventName", "事件名称", false, 20),
                build("eventDetail", "事件详情", false, 30)});

        ruleMap.put(PRE_PLAN_START.getId(), new SmsTmplateRule[]{
                build("name", "姓名", false, 4),
                build("time", "时间", false, 20),
                build("yuan", "预案", false, 10),
                build("phone", "电话", false, 15)});
    }

    public static String getTmpContext(String id) {
        SmsTmplateEnum smsTmplateEnum = templateMap.get(id);
        if (smsTmplateEnum != null) {
            return smsTmplateEnum.getTemplate();
        }
        return null;
    }

    public static void checkParam(JSONObject param, String templateId) {
        SmsTmplateEnum tmplateEnum = templateMap.get(templateId);
        if (tmplateEnum == null) {
            throw new SmsTmpParamException("短信模板不存在 " + templateId);
        }
        SmsTmplateRule.checkParam(param, ruleMap.get(templateId));
    }


}
